{{/* Function to retrieve the featured image */}}
{{/* Inputs: page context */}}
{{/* Output: image resource, or nil if not found */}}

{{/*
    Featured image is searched in this order:
      1. Search for a file `*featured*` in the post directory
      2. Search for a file `.Params.image.filename` in the post directory
      3. Search for a file `.Params.image.filename` in the `assets/media/` directory
*/}}

{{/* Search for an image "*featured*" in post folder */}}
{{ $resource := (.Resources.ByType "image").GetMatch "*featured*" }}
{{ if eq $resource nil }}
  {{/* Otherwise fall back the image file specified in front matter */}}
  {{ $filename := .Params.image.filename }}
  {{/* Search in post folder */}}
  {{ $resource = (.Resources.ByType "image").GetMatch $filename }}
  {{/* Otherwise in `assets/media/` folder */}}
  {{ if eq $resource nil }} {{ $resource = resources.GetMatch (path.Join "media" $filename) }} {{ end }}
{{ end }}

{{ return $resource }}
